﻿using Architecture.Repository.Impl.Traceability;
using Architecture.Repository.Interface.Traceability;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability.Domain.SupplierDomain;
using Traceability.Domain.TraceabilityDto;

namespace Traceability.Domain.Service.PackagingMaterialsServices
{
    /// <summary>
    /// 包材服务
    /// </summary>
    public class PackagingMaterialsService : IPackagingMaterialsService
    {
        /// <summary>
        /// 包材仓储
        /// </summary>
        protected readonly IPackagingMaterialsRepository packagingMaterialsRepository;

        /// <summary>
        /// 图片仓储
        /// </summary>
        protected readonly IPictureRepository pictureRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="packagingMaterialsRepository">包材仓储</param>
        /// <param name="pictureRepository">图片仓储</param>
        public PackagingMaterialsService(IPackagingMaterialsRepository packagingMaterialsRepository, IPictureRepository pictureRepository)
        {
            this.packagingMaterialsRepository = packagingMaterialsRepository;
            this.pictureRepository = pictureRepository;
        }

        public async Task<int> AddPackagingMaterials(PackagingMaterials packagingMaterials)
        {
            return await packagingMaterialsRepository.AddAsync(packagingMaterials);
        }

        public async Task<int> DeletePackagingMaterials(int packagingMaterialsId, string userMame)
        {
            var res = await packagingMaterialsRepository.GetModelAsync(packagingMaterialsId);
            res.DeletedBy = userMame;
            res.DeletedDate = DateTime.Now;
            res.IsDeleted = true;
            return await packagingMaterialsRepository.UpdateAsync(res);
        }

        public async Task<List<PackagingMaterialsDto>> GetAllPackagingMaterials(int shoppingId)
        {
            var list = (from a in await packagingMaterialsRepository.GetAllAsync()
                        join b in await pictureRepository.GetAllAsync()
                        on a.PackagingMaterialsId equals b.PackagingMaterialsId
                        where a.ShoppingId == shoppingId && !a.IsDeleted && !b.IsDeleted
                        select new PackagingMaterialsDto
                        {
                            PackagingMaterialsId = a.PackagingMaterialsId,
                            PackagingMaterialsProductionBatch = a.PackagingMaterialsProductionBatch,
                            PictureId = b.PictureId,
                            PictureName = b.PictureName,
                        }).ToList();
            return list;
        }

        public async Task<PackagingMaterials> GetPackagingMaterialsModel(int packagingMaterialsId)
        {
            return await packagingMaterialsRepository.GetModelAsync(packagingMaterialsId);
        }

        public async Task<int> UpdatePackagingMaterials(PackagingMaterials packagingMaterials)
        {
            var res = await packagingMaterialsRepository.GetModelAsync(packagingMaterials.PackagingMaterialsId);
            res.PackagingMaterialsId = packagingMaterials.PackagingMaterialsId;
            res.PackagingMaterialsProductionBatch= packagingMaterials.PackagingMaterialsProductionBatch;
            res.UpdatedBy = packagingMaterials.UpdatedBy;
            res.UpdatedDate = DateTime.Now;
            return await packagingMaterialsRepository.UpdateAsync(res);
        }
    }
}
